//
//  RealmMove.m
//  Realm-demo
//
//  Created by 小菜 on 17/5/8.
//  Copyright © 2017年 蔡凌云. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "DataMigration.h"
#import <Realm/Realm.h>

@interface RealmMove : XCTestCase

@end

@implementation RealmMove

- (void)setUp {
    [super setUp];
    // 迁移代码应该在appdelegate里面
    RLMRealmConfiguration *config = [RLMRealmConfiguration defaultConfiguration];
    
    // 升级版本
    int newVersion = 3;
    config.schemaVersion = newVersion;
    
    // 设置迁移block
    [config setMigrationBlock:^(RLMMigration *migration, uint64_t oldSchemaVersion){
        if (oldSchemaVersion < newVersion) {
            NSLog(@"数据库自动迁移了");
            
            // 执行更名动作
//            [migration renamePropertyForClass:@"DataMigration" oldName:@"fullname" newName:@"fullname2"];
            
            [migration enumerateObjects:@"DataMigration" block:^(RLMObject * _Nullable oldObject, RLMObject * _Nullable newObject) {
                newObject[@"fullName"] = [NSString stringWithFormat:@"%@-%@",oldObject[@"name"],oldObject[@"pet"]];
            }];
        }
    }];
    
    // 重新设置
    [RLMRealmConfiguration setDefaultConfiguration:config];
    // 立即生效
    [RLMRealm defaultRealm];
}


- (void)testExample {
//    DataMigration *m = [[DataMigration alloc] initWithValue:@[@1,@"2222",@"zhangsan"]];
//    [[RLMRealm defaultRealm] transactionWithBlock:^{
//        [[RLMRealm defaultRealm] addObject:m];
//    }];

    DataMigration *model = [[DataMigration alloc] init];
    model.num = 3;
    model.fullName = @"xxxxxxxx";
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        [[RLMRealm defaultRealm] addObject:model];
    }];
}


@end
